package com.ciban.smarthome.module.ui;

import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.SurfaceView;
import android.widget.Button;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.engine.MonitorDHEngine;
import com.ciban.smarthome.module.engine.MonitorDHEngine.MonitorListener;
import com.company.NetSDK.SDK_RealPlayType;

public class E_DHMonitorDetailsActivity extends BaseActivity implements OnClickListener {
	private SurfaceView play_view;
	private Button bt_capture;

	private SurfaceHolder holder;

	private MonitorDHEngine engine;

	private boolean isSvCreated = false, isStartRealPlay = false; // 主要用于解决视频
																	// 在一进入不显示的问题

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_equip_dhmonitor);

		initEngine();

		initViews();

		engine.setPlay_view(play_view);

		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				engine.monitorLogin();
			}
		}, 2000);
	}

	/**
	 * 初始化业务引擎
	 */
	private void initEngine() {
		engine = new MonitorDHEngine(this);
		engine.setListener(new MonitorListener() {

			@Override
			public void monitorLoginResult(boolean result, String errMsg) {
				if (result)
					play();
				else
					displayShort("登陆异常");
			}
		});
		engine.initMonitor();
	}

	private void initViews() {
		play_view = (SurfaceView) findViewById(R.id.monitor_show);
		bt_capture = (Button) findViewById(R.id.monitor_capture);

		bt_capture.setOnClickListener(this);

		holder = play_view.getHolder();
		holder.addCallback(new Callback() {

			public void surfaceCreated(SurfaceHolder holder) {
				LogUtil.d(E_DHMonitorDetailsActivity.class, "surfaceCreated");
				isSvCreated = true;
				if (isSvCreated && isStartRealPlay)
					engine.initSurface();
			}

			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
				LogUtil.d(E_DHMonitorDetailsActivity.class, "surfaceChanged");
			}

			public void surfaceDestroyed(SurfaceHolder holder) {
				LogUtil.d(E_DHMonitorDetailsActivity.class, "surfaceDestroyed");
			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.monitor_capture:
			if (engine.capture())
				displayShort("抓图成功");
			else
				displayShort("抓图失败");

			break;

		default:
			break;
		}
	}

	private void play() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Looper.prepare();
				if (engine.StartRealPlay(SDK_RealPlayType.SDK_RType_Realplay) == true) {
					handler.sendEmptyMessage(110);
					engine.addRealCallBack();
					isStartRealPlay = true;
					if (isSvCreated && isStartRealPlay)
						engine.initSurface();
				}
				Looper.loop();
			}
		}).start();
	}

	@Override
	protected void handMsg(Message msg) {
		super.handMsg(msg);
		switch (msg.what) {
		case 110:
			displayShort("开始播放");
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		engine.StopRealPlay();
		engine = null;
	}
}
